<template>
    <el-container>
        <el-header>
            <div class="left-panel">
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-date-picker v-model="search.date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <table border="1" style="border-collapse: collapse; width: 100%;">
                <thead>
                    <tr style="height:32px;line-height:32px;background:#f5f7fa;">
                        <th class="table_border">检验员</th>
                        <th class="table_border">品名</th>
                        <th class="table_border">入库单号</th>
                        <th class="table_border">匹数</th>
                        <th class="table_border">均重</th>
                        <th class="table_border">总重</th>
                        <th class="table_border">均码</th>
                        <th class="table_border">总码数</th>
                        <th class="table_border">米数不合格</th>
                        <th class="table_border">克重不合格</th>
                        <th class="table_border">杂活/活货</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- 检验员分组 -->
                    <template v-for="(inspectorGroup, inspectorIndex) in table" :key="`inspector-${inspectorIndex}`">
                        <!-- 品名分组 -->
                        <template v-for="(goodsGroup, goodsIndex) in inspectorGroup.goods" :key="`goods-${inspectorIndex}-${goodsIndex}`">
                            <!-- 入库单分组 -->
                            <template v-for="(warehouse, warehouseIndex) in goodsGroup.warehouses" :key="`warehouse-${inspectorIndex}-${goodsIndex}-${warehouseIndex}`">
                                <tr style="height:32px;line-height: 32px;">
                                    <!-- 检验员列 - 只在第一个品目的第一行显示 -->
                                    <td class="table_border" :rowspan="getTotalRowSpan(inspectorGroup)" v-if="goodsIndex === 0 && warehouseIndex === 0">
                                        {{inspectorGroup.inspector}}
                                    </td>
                                    <!-- 品名列 - 只在每个品目的第一行显示 -->
                                    <td class="table_border" :rowspan="goodsGroup.warehouses.length" v-if="warehouseIndex === 0">
                                        {{goodsGroup.goodsName}}
                                    </td>
                                    <!-- 入库单数据列 -->
                                    <td class="table_border">{{warehouse.warehouseCode}}</td>
                                    <td class="table_border">{{warehouse.number}}</td>
                                    <td class="table_border">{{warehouse.weight.toFixed(2)}}</td>
                                    <td class="table_border">{{warehouse.total_weight.toFixed(2)}}</td>
                                    <td class="table_border">{{warehouse.avg_code.toFixed(2)}}</td>
                                    <td class="table_border">{{warehouse.total_code.toFixed(2)}}</td>
                                    <td class="table_border">{{warehouse.meter_unqualified_count}}</td>
                                    <td class="table_border">{{warehouse.weight_unqualified_count}}</td>
                                    <td class="table_border">{{warehouse.miscellaneous_count}}</td>
                                </tr>
                            </template>
                        </template>
                        
                        <!-- 检验员小计行 -->
                        <tr style="height:32px;line-height: 32px; font-weight: bold;background-color: #dce6f1;">
                            <td class="table_border"></td>
                            <td colspan="2" class="table_border">小计</td>
                            <td class="table_border">{{inspectorGroup.number}}</td>
                            <td class="table_border">{{inspectorGroup.weight.toFixed(2)}}</td>
                            <td class="table_border">{{inspectorGroup.total_weight.toFixed(2)}}</td>
                            <td class="table_border">{{inspectorGroup.avg_code.toFixed(2)}}</td>
                            <td class="table_border">{{inspectorGroup.total_code.toFixed(2)}}</td>
                            <td class="table_border">{{inspectorGroup.meter_unqualified_count}}</td>
                            <td class="table_border">{{inspectorGroup.weight_unqualified_count}}</td>
                            <td class="table_border">{{inspectorGroup.miscellaneous_count}}</td>
                        </tr>
                    </template>
                    
                    <!-- 总计行 -->
                    <tr style="height:32px;line-height: 32px; font-weight: bold; background-color: #b1d0f0;">
                        <td colspan="3" class="table_border">总计</td>
                        <td class="table_border">{{total.number}}</td>
                        <td class="table_border"></td>
                        <td class="table_border">{{total.total_weight.toFixed(2)}}</td>
                        <td class="table_border"></td>
                        <td class="table_border">{{total.total_code.toFixed(2)}}</td>
                        <td class="table_border"></td>
                        <td class="table_border"></td>
                        <td class="table_border"></td>
                    </tr>
                </tbody>
            </table>
        </el-main>
    </el-container>
</template>

<script>

export default {
    name: '检验产量',
    data() {
            return {
                search: {
                    // date: ['2025-10-01', '2025-10-31'],
                    date: null,
                },
                table: [],
                total: {
                    number: 0,
                    weight: 0,
                    total_weight: 0,
                    avg_code: 0,
                    total_code: 0,
                    meter_unqualified_count: 0,
                    weight_unqualified_count: 0,
                    miscellaneous_count: 0
                }
            }
        },
    mounted() {
        // this.upsearch();
    },
    methods: {
        //搜索
        async upsearch() {
            var res = await this.$API.business.inspect.output.get(this.search);
            if(res.code == 1){
                // 原始数据
                const originalData = res.data;
                
                // 按检验员、品名、入库单号进行多级分组
                const inspectorMap = new Map();
                
                originalData.forEach(item => {
                    const inspector = item.work_uids_name;
                    const goodsName = item.goods_name;
                    const warehouseCode = item.purchaseware?.purchase_ware_code || '';
                    
                    // 检验员分组
                    if (!inspectorMap.has(inspector)) {
                        inspectorMap.set(inspector, {
                            inspector: inspector,
                            goods: [],
                            // 初始化检验员级别的统计数据
                            number: 0,
                            weight: 0,
                            total_weight: 0,
                            avg_code: 0,
                            total_code: 0,
                            meter_unqualified_count: 0,
                            weight_unqualified_count: 0,
                            miscellaneous_count: 0
                        });
                    }
                    
                    const inspectorGroup = inspectorMap.get(inspector);
                    
                    // 品名单分组
                    let goodsGroup = inspectorGroup.goods.find(g => g.goodsName === goodsName);
                    if (!goodsGroup) {
                        goodsGroup = {
                            goodsName: goodsName,
                            warehouses: [],
                            // 初始化品名单级别的统计数据
                            number: 0,
                            weight: 0,
                            total_weight: 0,
                            avg_code: 0,
                            total_code: 0,
                            meter_unqualified_count: 0,
                            weight_unqualified_count: 0,
                            miscellaneous_count: 0
                        };
                        inspectorGroup.goods.push(goodsGroup);
                    }
                    
                    // 入库单分组
                    let warehouseItem = goodsGroup.warehouses.find(w => w.warehouseCode === warehouseCode);
                    if (!warehouseItem) {
                        warehouseItem = {
                            warehouseCode: warehouseCode,
                            number: 0,
                            weight: 0,
                            total_weight: 0,
                            avg_code: 0,
                            total_code: 0,
                            meter_unqualified_count: 0,
                            weight_unqualified_count: 0,
                            miscellaneous_count: 0
                        };
                        goodsGroup.warehouses.push(warehouseItem);
                    }
                    
                    // 更新入库单统计数据
                    const itemNumber = Number(item.number) || 0;
                    const itemTotalWeight = Number(item.weight) || 0;
                    const itemTotalCode = Number(item.code_number) || 0;
                    const itemMeterUnqualified = Number(item.meter_unqualified_count) || 0;
                    const itemWeightUnqualified = Number(item.weight_unqualified_count) || 0;
                    const itemMiscellaneous = Number(item.miscellaneous_count) || 0;
                    
                    warehouseItem.number += itemNumber;
                    warehouseItem.total_weight += itemTotalWeight;
                    warehouseItem.total_code += itemTotalCode;
                    warehouseItem.meter_unqualified_count += itemMeterUnqualified;
                    warehouseItem.weight_unqualified_count += itemWeightUnqualified;
                    warehouseItem.miscellaneous_count += itemMiscellaneous;
                    
                    // 重新计算入库单平均值
                    warehouseItem.weight = warehouseItem.total_weight / warehouseItem.number || 0;
                    warehouseItem.avg_code = warehouseItem.total_code / warehouseItem.number || 0;
                    
                    // 更新品名单统计数据
                    goodsGroup.number += itemNumber;
                    goodsGroup.total_weight += itemTotalWeight;
                    goodsGroup.total_code += itemTotalCode;
                    goodsGroup.meter_unqualified_count += itemMeterUnqualified;
                    goodsGroup.weight_unqualified_count += itemWeightUnqualified;
                    goodsGroup.miscellaneous_count += itemMiscellaneous;
                    
                    // 重新计算品名单平均值
                    goodsGroup.weight = goodsGroup.total_weight / goodsGroup.number || 0;
                    goodsGroup.avg_code = goodsGroup.total_code / goodsGroup.number || 0;
                    
                    // 更新检验员统计数据
                    inspectorGroup.number += itemNumber;
                    inspectorGroup.total_weight += itemTotalWeight;
                    inspectorGroup.total_code += itemTotalCode;
                    inspectorGroup.meter_unqualified_count += itemMeterUnqualified;
                    inspectorGroup.weight_unqualified_count += itemWeightUnqualified;
                    inspectorGroup.miscellaneous_count += itemMiscellaneous;
                    
                    // 重新计算检验员平均值
                    inspectorGroup.weight = inspectorGroup.total_weight / inspectorGroup.number || 0;
                    inspectorGroup.avg_code = inspectorGroup.total_code / inspectorGroup.number || 0;
                });
                
                // 将Map转换为数组
                this.table = Array.from(inspectorMap.values());
                this.getTotal()
            }
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.upsearch();
        },
        // 计算检验员需要合并的行数
        getTotalRowSpan(inspectorGroup) {
            let rowSpan = 0;
            // 计算所有入库单行数
            inspectorGroup.goods.forEach(goods => {
                rowSpan += goods.warehouses.length;
            });
            rowSpan += 1; // 增加检验员小计行
            return rowSpan - 1;
        },
        // 计算总计
        getTotal() {
            this.total = {
                number: 0,
                weight: 0,
                total_weight: 0,
                avg_code: 0,
                total_code: 0,
                meter_unqualified_count: 0,
                weight_unqualified_count: 0,
                miscellaneous_count: 0
            };
            
            this.table.forEach(inspectorGroup => {
                this.total.number += inspectorGroup.number;
                this.total.total_weight += inspectorGroup.total_weight;
                this.total.total_code += inspectorGroup.total_code;
                this.total.meter_unqualified_count += inspectorGroup.meter_unqualified_count;
                this.total.weight_unqualified_count += inspectorGroup.weight_unqualified_count;
                this.total.miscellaneous_count += inspectorGroup.miscellaneous_count;
            });
            
            // 计算平均
            if (this.total.number > 0) {
                this.total.weight = this.total.total_weight / this.total.number;
                this.total.avg_code = this.total.total_code / this.total.number;
            }
        }
    },
}
</script>

<style lang="scss">
td{
    text-align: center;
}
.table_border{
    width: 80px;
    height: 32px;
    // border: 1px solid #000;
}
table{
    tbody{
        font-size: 13px;
        tr {
            page-break-inside: avoid;
            -webkit-region-break-inside: avoid; 
        }
        tr:last-child{
            page-break-after: avoid;
            
        }
    }
} 
</style>
